/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author pyin
 */
public class CommandInvoker {

    public static boolean hasSuchMethod(Class klass, String method) {
        Method[] theMethods = klass.getDeclaredMethods();
        for (int i = 0; i < theMethods.length; i++) {
            String methodString = theMethods[i].getName();
//			System.out.println(methodString);
            if (methodString.equals(method)) {
                return true;
            }
        // System.out.println("Name: " + methodString);
        // String returnString = theMethods[i].getReturnType().getName();
        // System.out.println(" Return Type: " + returnString);
        // Class[] parameterTypes = theMethods[i].getParameterTypes();
        // System.out.print(" Parameter Types:");
        // for (int k = 0; k < parameterTypes.length; k++) {
        // String parameterString = parameterTypes[k].getName();
        // System.out.print(" " + parameterString);
        // }
        // System.out.println();
        }
        return false;
    }

    public static boolean isValidCommand(Command obj, String cmd) {
        String [] patterns = obj.getPatterns();
        for (int i = 0; i < patterns.length; i++) {
            Pattern p = Pattern.compile(patterns[i]);
            Matcher m = p.matcher(cmd.toLowerCase());
            if (m.find()) {
                return true;
            }
        }
        return false;
    }

    public static Object executeMethod(Command obj, String cmd) throws InvalidCommandException{
        String[] patterns = obj.getPatterns();
        for (int i = 0; i < patterns.length; i++) {
            Pattern p = Pattern.compile(patterns[i]);
            Matcher m = p.matcher(cmd.toLowerCase());
            if (m.find()) {
                String method = m.group(1);
                String[] args = new String[m.groupCount() - 1];
                for (int j = 0; j < args.length; j++) {
                    args[j] = m.group(j + 2);
                }
                if (hasSuchMethod(obj.getClass(), method)) {
                    return runMethod(obj, method, args);
                }
            }
        }
        throw new InvalidCommandException();
    }

    private static Object runMethod(Command obj, String method, Object[] params) {
        Class klass = obj.getClass();
        Method[] methods = klass.getMethods();
        for (int i = 0; i < methods.length; i++) {
            String methodString = methods[i].getName();
            if (methodString.equalsIgnoreCase(method)) {
                try {
                    return methods[i].invoke(obj, params);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void main(String [] args){
        try {
            Command cmd = new Command();
            CommandInvoker.executeMethod(cmd, "setup sybase-ee702900");
            //System.out.println(CommandInvoker.executeMethod(cmd, "qm win03uepinstall"));
        } catch (InvalidCommandException ex) {
            Logger.getLogger(CommandInvoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
